<?php 
/*
**模型：微博客
**model/class.blog.php
**By QINIAO
*/

class qiMlog{
	var $db;
	
	function qiMlog($dbhandle)
	{
		$this->db = $dbhandle;
	}
	
	//显示所有微薄
	function getMlog($page = 1, $prePageNum)
	{
	$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
	$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
	$res = $this->db->query("select * from ".DB_PREFIX."mlogs order by cTime desc $limit");
	$mlogs = array();
	while($row = $this->db->fetch_array($res))
	{
	$mlogs[] = $row;
	}
	return $mlogs;
	}
	//rss显示微博
	function getMlogForRss()
	{
	$res = $this->db->query("select * from ".DB_PREFIX."mlogs order by cTime desc limit 20");
	$mlogs = array();
	while($row = $this->db->fetch_array($res))
	{
	$mlogs[] = $row;
	}
	return $mlogs;
	}
	//获取指定话题的微博列表
	function getCateMlog($page = 1,$prePageNum,$cid)
	{
	$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
	$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
	$res = $this->db->query("select * from ".DB_PREFIX."mlogs where cid=$cid order by cTime desc $limit");
	$mlogs = array();
	while($row = $this->db->fetch_array($res))
	{
	$mlogs[] = $row;
	}
	return $mlogs;
	}
	
	//获取指定用户的微博列表
	function getUserMlog($page = 1,$prePageNum,$userid)
	{
	$start_limit = !empty($page) ? ($page - 1) * $prePageNum : 0;
	$limit = $prePageNum ? "LIMIT $start_limit, $prePageNum" : '';
	$res = $this->db->query("select * from ".DB_PREFIX."mlogs where uid=$userid order by cTime desc $limit");
	$mlogs = array();
	while($row = $this->db->fetch_array($res))
	{
	$mlogs[] = $row;
	}
	return $mlogs;
	}
	
	//发布微博
	function addMlog($cid, $uid, $content,$aid,$ctime)
	{
	$sql = "insert into ".DB_PREFIX."mlogs (cid, uid, content,aid,cTime) values ('$cid', '$uid', '$content', '$aid','$ctime')";
	$this->db->query($sql);
	}
		
	//获取单条微博
	function getOneMlog($logid)
	{
	$res = $this->db->query("select * from ".DB_PREFIX."mlogs where id=$logid");
	$row = $this->db->fetch_array($res);
	if($row < 1)
	{
	redirect('index.php', 1, '不好意思，你查看的微博不存在!3秒钟后跳转到首页！'); 
	}
	elseif($row)
	{
	$logDate = array(
	'id' => $row['id'],
	'content' => htmlspecialchars($row['content']),
	'cid' => $row['cid'],
	'uid' => $row['uid'],
	'aid' => $row['aid'],
	'cTime' => $row['cTime']
	);
	return $logDate;
	}
	}
	
	//更新日志内容
	function updateLog($logData, $blogId)
	{
	$Item = array();
	foreach ($logData as $key => $data)
	{
		$Item[] = "$key='$data'";
	}
	$upStr = implode(',', $Item);
	$this->db->query("update ".DB_PREFIX."mlogs set $upStr where id=$blogId");
	}
	
	//获取日志条数
	function getLogNum($virtue, $setvirtue)
	{
	$where = 'where '.$virtue.'='.$setvirtue.'';
	$res = $this->db->query("SELECT * FROM ".DB_PREFIX."mlogs $where" );
	$LogNum = $this->db->num_rows($res);
	return $LogNum;
	}
	
	//删除微博
	function deleteMlog($logid)
	{
	$this->db->query("delete from ".DB_PREFIX."mlogs where id=$logid");
	}
	
	}

?>